//
//  YSpreadViewItemView.m
//  yuimall_Iphone
//
//  Created by yuike6 yuike6 on 12-4-18.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YSpreadViewItemView.h"
#import "header.h"
#import "YSpreadViewItemViewSubViewItemView.h"



@interface YSpreadViewItemView (private)
-(void) fillData2Views;
-(CGRect) subItemPanlFrame;

-(void) reAdjustSubItemViewsPanlLayout;
@end

@implementation YSpreadViewItemView
@synthesize data = data_;
@synthesize subjectView = subjectView_;

@synthesize delegate = delegate_;

@synthesize itemViews = itemViews_;
@synthesize subItemPanl;

-(void) dealloc
{
    YRelease(data_);
    YRelease(subjectView_)
    
    YRelease(subItemPanl)
    
    YRelease(itemViews_);
    [super dealloc];
}

-(void) setData:(YSpreadViewModel *)dataValue
{
    YRelease(data_)
    data_ = [dataValue retain];
 
    [self fillData2Views];
    
    [self reAdjustSubItemViewsPanlLayout];
}


-(void) privateInit
{
    if (self.bInited) 
    {
        return;
    }
    
    YSpreadViewItemViewSubjectView * tmp_subject = [[YSpreadViewItemViewSubjectView alloc] init];
    [tmp_subject setDelegate:self];
    self.subjectView = tmp_subject;
    YRelease(tmp_subject);
    [self addSubview:self.subjectView];
    
    UIViewEx * tmp_view = [[UIViewEx alloc] init];
    
    self.subItemPanl = tmp_view;
    YRelease(tmp_view);
    [self addSubview:self.subItemPanl];
    YHIDEVIEW(self.subItemPanl);
        
    NSMutableArray * tmp_itemViews = [[NSMutableArray alloc] init];
    self.itemViews = tmp_itemViews;
    YRelease(tmp_itemViews);
    
    [self reAdjustLayout];    
    
    self.bInited = TRUE;
}

-(id) init
{
    self = [super init];
    if (self) {
        [self privateInit];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

-(void) setFrame:(CGRect)frame
{
    [super setFrame:frame];    
    [self reAdjustLayout];
   
}

-(void) reAdjustLayout
{
    [self.subjectView setFrame:CGRectMake(0, 0, self.width, _SUBJECT_ITEM_HEIGHT)];
    
    [self reAdjustSubItemViewsPanlLayout];
}


-(void) reAdjustSubItemViewsPanlLayout
{
    [self.subItemPanl setFrame:[self subItemPanlFrame]];
    
    int flag = 0;
    for (UIView * view in self.itemViews) {
         [view setFrame:[self itemFrame:flag]];
        flag++;
    }
}



-(CGRect) itemFrame:(NSInteger) itemIndex
{
    CGRect frame = CGRectZero;
    
    frame.origin.x = 0;
    frame.origin.y = _ITEM_HEIGHT_ * itemIndex;
    
    frame.size.width = VIEW_WIDTH;
    frame.size.height = _ITEM_HEIGHT_;
    
    return frame;
}

-(void) fillData2Views
{
    [self.subjectView setItemModel:self.data.subject];
    
    [self.itemViews removeAllObjects];
    
    for (YSpreadViewItemModel * itemData in self.data.subItems) {
        YSpreadViewItemViewSubViewItemView * itemView = [[YSpreadViewItemViewSubViewItemView alloc] init];
        
        [itemView setTag:[self.itemViews count]];
        [itemView setData:itemData];
        [itemView setDelegate:self];
        
        
        [itemView setFrame:[self itemFrame:[self.itemViews count]]];
        
        [self.subItemPanl addSubview:itemView];
        [self.itemViews addObject:itemView];
        
        YRelease(itemView)        
    }
}

-(CGFloat) getAllSubItemViewHeight
{
    CGFloat height = 0;
    for (UIView * view in self.itemViews) {
        height += view.frame.size.height;
    }
        
    return height;
}


-(CGRect) subItemPanlFrame
{
    CGRect frame = CGRectZero;
    
    frame.origin.x = 0;
    frame.origin.y = self.subjectView.origin_y + self.subjectView.height;
    
    frame.size.width = self.width;
    
    CGFloat height = 0;
    height = [self getAllSubItemViewHeight];
    frame.size.height = height;
    
    
    return frame;
}


-(void) reAdjustMyselfFrame:(BOOL) spreaded
{
    CGRect frame = self.frame;
    
    if (spreaded) 
    {
        frame.size.height = self.subjectView.height + self.subItemPanl.height;
    }
    else
    {
        frame.size.height = self.subjectView.height;
    }
    
    [self setFrame:frame];
}

#pragma mark -
#pragma mark - YSpreadViewItemViewSubViewItemViewDelegate
- (void) clickSubItem:(YSpreadViewItemViewSubViewItemView *) itemView
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(clickWhichSubItem:subItemView:)]) {
        [self.delegate clickWhichSubItem:self.subjectView subItemView:itemView];
    }
}

- (void) subjectImageClick:(YSpreadViewItemViewSubjectView *) subjectView
{
    DELEGATE_CALLBACK(subjectImageClick:, subjectView);
}

#pragma mark -
#pragma mark - YSpreadViewItemViewSubjectViewDelegate
-(void) clickSubjectView:(YSpreadViewItemViewSubjectView *) subjectView
{
    self->isSpread = !self->isSpread;
    
    [self reAdjustMyselfFrame:self->isSpread];
    
    if (self->isSpread) 
    {
        YSHOWVIEW(self.subItemPanl);       
        DELEGATE_CALLBACK(spread:, self);
    }
    else 
    {
        DELEGATE_CALLBACK(restore:, self);
//        YHIDEVIEW(self.subItemPanl);
    }    
}

-(BOOL) isNeedShowHighLight:(YSpreadViewItemViewSubjectView *) subjectView
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(isNeedShowHighLight:)]) {
        return [self.delegate isNeedShowHighLight:subjectView];
    }
    return FALSE;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

@interface YSpreadViewItemViewSubjectView (private)
-(void) fillData2Views;
@end


@implementation YSpreadViewItemViewSubjectView

@synthesize imageView = imageView_;
@synthesize title = title_;

@synthesize flagImageView = flagImageView_;
@synthesize itemModel = itemModel_;

@synthesize delegate = delegate_;

@synthesize bgImageView = bgImageView_;
@synthesize isHighLight;

-(void) dealloc
{
    YRelease(imageView_)
    YRelease(title_)
    
    YRelease(flagImageView_)
    YRelease(itemModel_);
    
    YRelease(bgImageView_)
    
    [super dealloc];
}

- (void) setIsHighLight:(BOOL)isHighLightValue
{
    isHighLight = isHighLightValue;
    
    if (isHighLight) {
        [self.imageView setImage:self.itemModel.highlightImage forState:UIControlStateNormal];
        [self.title setTextColor:COLOR_BLACK];
    }
    else {
        [self.imageView setImage:self.itemModel.image forState:UIControlStateNormal];
        
        [self.title setTextColor:RGB(180, 180, 180)];
    }
}

-(void) setItemModel:(YSpreadViewItemModel *)itemModelValue
{
    YRelease(itemModel_)
    itemModel_ = [itemModelValue retain];
    
    [self fillData2Views];
}


-(void) privateInit
{
    if (self.bInited) {
        return;
    }
    
    UIImageView * tmp_bgImageview= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_spreadview.png"]];
    self.bgImageView = tmp_bgImageview;
    YRelease(tmp_bgImageview);
    [self addSubview:self.bgImageView];
    
    self.imageView = [UIButton buttonWithType:UIButtonTypeCustom];
    BUTTON_FUNCTION(self.imageView, subjectClick);
    [self addSubview:self.imageView];
    
    UILabel * tmp_lable = [[UILabel alloc] init];
    [tmp_lable setBackgroundColor:COLOR_CLEAR];
    self.title = tmp_lable;
    YRelease(tmp_lable);
    [self addSubview:self.title];
    
    UIImageView * tmp_flagImageView = [[UIImageView alloc] init];
    [tmp_flagImageView setImage:[UIImage imageNamed:@"spreadflagNormal.png"]];
    self.flagImageView = tmp_flagImageView;
    YRelease(tmp_flagImageView)
    [self addSubview:self.flagImageView];
       
    
    [self reAdjustLayout];    
    self.bInited = TRUE;
}

-(id) init
{
    self = [super init];
    if (self) {
        [self privateInit];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}


-(void) reAdjustLayout
{
    [self.bgImageView setFrame:self.bounds];
    
    [self.imageView setFrame:CGRectMake(5, 5, self.height - 10, self.height - 10)];
    [self.title setFrame:CGRectMake(60, 5, self.width - 60 - 30, self.height - 10)];
    
    [self.flagImageView setFrame:CGRectMake(self.width - 60, (self.height - 50) / 2, 37, 50)];
}

-(void) setFrame:(CGRect)frame
{
    [super setFrame:frame];    
    [self reAdjustLayout];
}

- (void) subjectClick
{
    self.isHighLight = !self.isHighLight;
    DELEGATE_CALLBACK(subjectImageClick:, self)
}
-(void) fillData2Views
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(isNeedShowHighLight:)]) {
        if ([self.delegate isNeedShowHighLight:self]) {
            self.isHighLight = TRUE;
        }
        else {
            self.isHighLight = FALSE;
        }
    }
 
    [self.title setText:self.itemModel.title];
}


-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{    
        
    self->isSpread = !self->isSpread;
    if (self->isSpread) {
        [self.flagImageView setImage:[UIImage imageNamed:@"spreadflagOpen.png"]];
    }
    else {
        [self.flagImageView setImage:[UIImage imageNamed:@"spreadflagNormal.png"]];
    }
        
    [super touchesEnded:touches withEvent:event];
    DELEGATE_CALLBACK(clickSubjectView:, self);
}
@end


























